package com.lovo.Atm.frame;

import com.lovo.Atm.pojo.UserDO;
import com.lovo.Atm.service.UserService;
import com.lovo.Atm.service.impl.UserServiceImpl;
import com.lovo.Atm.util.JDUtil;
import com.lovo.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Transfer extends JPanel implements ActionListener {
    private Home frame;
    private JButton backImg;
    private JButton sureImg;
    private JLabel noteAccount;
    private JLabel noteBlance;
    private JTextField printAcc;
    private JTextField printBla;
    private Icon backBtn = Util.iconImg("/images/buttonOff.jpg");
    private Icon sureBtn = Util.iconImg("/images/buttonSure.jpg");
    private UserService userService = new UserServiceImpl();

    public Transfer(Home frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        //用户名提示和输入框
        noteAccount = new JLabel("请输入转账用户名：");
        noteAccount.setFont(new Font("宋体", Font.BOLD, 20));
        noteAccount.setBounds(350, 200, 250, 30);
        this.add(noteAccount);

        printAcc = new JTextField();
        printAcc.setBounds(620, 200, 100, 30);
        this.add(printAcc);

        //转账金额
        noteBlance = new JLabel("请输入转账金额：");
        noteBlance.setFont(new Font("宋体", Font.BOLD, 20));
        noteBlance.setBounds(350, 250, 250, 30);
        this.add(noteBlance);

        printBla = new JTextField();
        printBla.setBounds(620, 250, 100, 30);
        this.add(printBla);

        //确认按钮
        sureImg = new JButton(sureBtn);
        sureImg.setBounds(0, 400, 140, 50);
        this.add(sureImg);
        sureImg.addActionListener(this);

        //退出按钮
        backImg = new JButton(backBtn);
        backImg.setBounds(870, 400, 140, 50);
        this.add(backImg);
        backImg.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (sureImg == e.getSource()) {
            //转账金额
            Double transferMoney = Double.valueOf(printBla.getText());
            try {
                userService.transfer(frame.getLoginUser().getId(), printAcc.getText(), transferMoney);
                JOptionPane.showMessageDialog(this, "转账成功");
                frame.switchPanel("menu");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
                printAcc.setText("");
                printBla.setText("");
            }
        } else if (e.getSource() == backBtn) {
            frame.switchPanel("menu");
            printAcc.setText("");
            printBla.setText("");

        }
    }
}


